<?php
class Con_cmd extends Acan{

	public $defaultAct='run';
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
	}

	function Act_run(){
		include $this->Atpl();
	}
	function Act_runcmd(){
		if(Afun_get_os()=='linux'){
			echo Afun_run_cmd($_POST['cmd']); 
		}else{
			echo Afun_run_cmd($_POST['cmd']); 
		}
	}
	//--ip统计
	function Act_stat_ip(){
		include $this->Atpl();
	}
	function Act_stat_ip_ajax(){
		$data = Afun_new('shell')->netstat_ip($_POST['port']);
		include $this->Atpl();
	}
	//tasklist
	function Act_tasklist(){
		$tasklist = explode("\n",Afun_run_cmd("tasklist"));
		$tasknum = count($tasklist);
		
		$page->theurl = $this->Aactr.'-'.$this->id;
		$page->by = 'pid ASC';
		$page->page = '20';
		$page->counts = $tasknum;	//统计总数目
		$pages = Afun_apage($page);
		$taskth = $this->taskinfo($tasklist[1]);
		for ($i=3; $i<$tasknum; $i++) {
			$tinfo = $this->taskinfo($tasklist[$i]);
			if(empty($tinfo['pid'])){continue;}
			$total['memory'] += $tinfo['memory'];
			$data[] = $tinfo;
		}
		//--排序数组
		$data = Afun_arr_sort($data,$pages->by,$pages->order);
		//--分页数组
		$irr[0] = $pages->start;
		$irr[1] = $pages->page;
		if(is_array($data)){
			unset($i,$val);
			foreach($data as $i=>$val){
				$val['id'] = $i+1;
				if($i>=(($irr[0]-1)*$irr[1]) && $i<($irr[0]*$irr[1])){
					$val['memory'] = Afun_format_size($val['memory']*1024);
					$datas[] = $val;
				}
			}
		}
		$total['memory'] = Afun_format_size($total['memory']*1024);
		include $this->Atpl();
	}
	//taskkill
	function Act_taskkill($pid='',$taskname='',$str=''){
		if($_POST['pid']){
			$pid = $_POST['pid'];
		}elseif($_POST['taskname']){
			$taskname = $_POST['taskname'];
		}
		if($_POST['str']){$str = $_POST['str'];}
		if($pid) $cmd = "TASKKILL /PID $pid /F $str";
		if($taskname) $cmd = "TASKKILL /F /IM $taskname $str";
		echo $cmd;
		echo Afun_run_cmd($cmd);
	}
	
	function taskinfo($str){
		$vrr = explode('&&',preg_replace('/([\s]+)/','&&',$str));
		$vnum = count($vrr);
		if($vnum==6){
			$vinfo['taskname'] = $vrr[0];
		}else{
			$vinfo['taskname'] = $this->uni_task($vrr,$vnum-5);
		}
		$vinfo['pid'] = $vrr[$vnum-5];
		$vinfo['type'] = $vrr[$vnum-4];
		$vinfo['session'] = $vrr[$vnum-3];
		$vinfo['memory'] = str_replace(',','',$vrr[$vnum-2]);
		$vinfo['size'] = $vrr[$vnum-1];
		return $vinfo;
	}
	function uni_task($arr,$num){
		for ($i=0; $i < $num; $i++) { 
			if(empty($val)){
				$val = $arr[$i];
			}else{
				$val .= ' '.$arr[$i];
			}
		}
		return $val;
	}
}

?>